package com.axzhengxin.platform.afterloan.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ssh.JschUtil;
import com.axzhengxin.platform.afterloan.domain.FspCorInf;
import com.axzhengxin.platform.common.props.AuthFileSftpProps;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * IWarningTaskService
 * 贷后预警定时任务服务
 *
 * @author jiangnan
 * @date 2021/3/8
 */
public interface IWarningTaskService {

    Logger LOGGER = LoggerFactory.getLogger(IWarningTaskService.class);

    /**
     * 二进制流缓冲区大小（默认1MB）
     */
    int AUTH_FILE_CACHE = 1024 * 1024 * 1024;

    /**
     * 分页调用的每页大小
     */
    int PAGE_SIZE = 100;

    /**
     * 无参任务方法
     */
    void exec();

    /**
     * 读取授权书信息
     *
     * @param data              请求报文对象
     * @param authFileSftpProps 授权sftp相关参数
     */
    default void fillAuthFileContent(RiskModelRequestData data, AuthFileSftpProps authFileSftpProps) {
        Session session = null;
        ChannelSftp channelSftp = null;
        String fileContent = "";
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(AUTH_FILE_CACHE);
        try {
            session = JschUtil.getSession(authFileSftpProps.getHost(),
                    authFileSftpProps.getPort(),
                    authFileSftpProps.getUsername(),
                    authFileSftpProps.getPassword());
            channelSftp = JschUtil.openSftp(session);
            channelSftp.get(data.getFilePath(), outputStream);
            fileContent = StrUtil.trimToEmpty(
                    new String(
                            Base64.encodeBase64(outputStream.toByteArray()), StandardCharsets.UTF_8));
            LOGGER.info("读取授权书文件(Base64编码)长度:{}", fileContent.length());
        } catch (Exception ex) {
            LOGGER.error("读取授权书文件发生异常, 文件路径为:" + data.getFilePath(), ex);
        } finally {
            if (channelSftp != null) {
                channelSftp.exit();
            }
            if (session != null) {
                session.disconnect();
            }
        }
        data.setFileContent(fileContent);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    class RiskModelRequest {

        private List<RiskModelRequestData> data;

    }

    /**
     * 预警信息查询请求报文体实体类
     */
    @Data
    @NoArgsConstructor
    class RiskModelRequestData {

        /**
         * 用户ID
         */
        private String rcrUserNo;

        /**
         * 企业名称
         */
        private String entName;

        /**
         * 业务流水号
         */
        private String transSerialNo;

        /**
         * 法人名称
         */
        private String lerep;

        /**
         * 法人代表证件号
         */
        private String cerno;

        /**
         * 法人代表电话号码
         */
        private String phonenum;

        /**
         * 统一社会信用代码
         */
        private String uniscid;

        /**
         * 企业授权书路径
         */
        private String filePath;

        /**
         * 企业授权书内容，BASE64编码
         */
        private String fileContent;

        public RiskModelRequestData(FspCorInf fspCorInf) {
            super();
            this.rcrUserNo = fspCorInf.getId();
            this.entName = fspCorInf.getName();
            this.lerep = fspCorInf.getLegalPerson();
            this.cerno = fspCorInf.getLegalPersonCertId();
            this.phonenum = fspCorInf.getLegalPersonPhone();
            this.uniscid = fspCorInf.getUniscId();
            this.filePath = fspCorInf.getAuthFilePath();
        }

    }

}
